Ext.define('YXMES.smat.controller.WorkOrderRequestController', {
	extend : 'Ext.app.Controller',
	refs : [ {
		selector : 'smat_workorderrequest',
		ref : 'smatworkorderrequest'
	} ],
	requires : [ 'YXMES.smat.store.msnForMoList', 'YXMES.smat.store.WarehouseStore', 'YXMES.smat.store.WorkOrderStore' ],
	stores : [],
	models : [],
	views : [ 'YXMES.smat.view.WorkOrderRequestView' ],
	subcontrollers : [  ],
	_init : false,
	init : function() {
		if (this._init)
			return;
		this._init = true;
		this.control({
			'smat_workorderrequest' : {
				afterrender : function() {
					return this.handle(this.onAfterrender, arguments);
				},
				destroy : function() {
					return this.handle(this.onDestroy, arguments);
				}
			},
			'smat_workorderrequest #cdvMo' : {
				select : function() {
					return this.handle(this.onCdvMoSelect, arguments);
				}
			},
			'smat_workorderrequest #txtMsn' : {
				specialkey : function() {
					return this.handle(this.onTxtMsnSpecialkey, arguments);
				}
			},
			'smat_workorderrequest #btnRefresh' : {
				click : function() {
					return this.handle(this.onBtnRefreshClick, arguments);
				}
			},
			'smat_workorderrequest #cmbIoType' : {
				select : function() {
					return this.handle(this.onCmbIoTypeSelect, arguments);
				}
			}
		});
		if (!Ext.isEmpty(this.subcontrollers)) {
			Ext.each(this.subcontrollers, function(name) {
				this.getController(name).init();
			}, this);
		}
		// User definied init
	},
	mainView : undefined,
	getMainView : function() {
		return this.mainView;
	},
	getScopeView : function() {
		return this.getMainView().scopeView || {};
	},
	getInput : function() {
		return this.getScopeView().input || {};
	},
	setInput : function(data) {
		this.getMainView().input = data;
	},
	getOutput : function() {
		return this.getMainView().output || {};
	},
	setOutput : function(data) {
		this.getScopeView().output = data;
	},
	handle : function(func, args) {
		var item = args && args.length > 0 && Ext.typeOf(args[0]) === 'object' ? args[0] : undefined;
		if (item) {
			try {
				this.mainView = (item.xtype == 'smat_workorderrequest' ? item : item.up('smat_workorderrequest'));
			} catch(e) {
			}
		}
		try {
			return func.apply(this, args);
		} finally {
		}
	},
	getItem : function(itemId) {
		return itemId===this.getMainView().getItemId()?this.getMainView():this.getMainView().down('#' + itemId);
	},
	getStore : function(name) {
		if (name == 'msnForMoList')
			return this.getMainView().SmfrViewMaterialSnWaittingForOrderServiceStore;
		if (name == 'IOTypeStore')
			return this.getMainView().iOTypeStoreStore;
		if (name == 'WarehouseStore')
			return this.getMainView().SbasViewWarehouseDefinitionServiceStore;
		if (name == 'WorkOrderStore')
			return this.getMainView().SmfrViewProductionOrderServiceStore;
		if (name == 'MoRequestScanResultStore')
			return this.getMainView().moRequestScanResultStoreStore;
		return undefined;
	},
	bind : function(view) {
		view = (view ? view : this.getMainView());
		view.SmfrViewMaterialSnWaittingForOrderServiceStore = Ext.create('YXMES.smat.store.msnForMoList', {
			pageSize : 100
		});
		view.iOTypeStoreStore = Ext.create('Ext.data.ArrayStore', {
			pageSize : 30,
			fields : [ {
				name : 'k',
				type : 'string'
			}, {
				name : 't',
				type : 'string'
			} ],
			data : [
				[ 'O', '出库' ],
				[ 'I', '退库' ]
			]
		});
		view.SbasViewWarehouseDefinitionServiceStore = Ext.create('YXMES.smat.store.WarehouseStore', {
			pageSize : 30,
			autoDestory : true,
			autoLoad : true
		});
		view.SmfrViewProductionOrderServiceStore = Ext.create('YXMES.smat.store.WorkOrderStore', {
			pageSize : 30
		});
		view.moRequestScanResultStoreStore = Ext.create('Ext.data.ArrayStore', {
			pageSize : 30,
			fields : [ {
				name : 'scanTime',
				type : 'string'
			}, {
				name : 'mo',
				type : 'string'
			}, {
				name : 'matCode',
				type : 'string'
			}, {
				name : 'matDesc',
				type : 'string'
			}, {
				name : 'ioType',
				type : 'string'
			}, {
				name : 'whName',
				type : 'string'
			}, {
				name : 'msn',
				type : 'string'
			}, {
				name : 'qty',
				type : 'string'
			}, {
				name : 'result',
				type : 'string'
			}, {
				name : 'resultText',
				type : 'string'
			} ]
		});
		view.down('#grdWait').reconfigure(view.SmfrViewMaterialSnWaittingForOrderServiceStore);
		view.down('#cmbIoType').bindStore(view.iOTypeStoreStore);
		view.down('#cmbWhCode').bindStore(view.SbasViewWarehouseDefinitionServiceStore);
		view.down('#cdvMo').bindStore(view.SmfrViewProductionOrderServiceStore);
		view.down('#grdScanResult').reconfigure(view.moRequestScanResultStoreStore);
	},
	onAfterrender : function(view, eOpts) {
		this.bind();
		// User defined afterrender
	},
	onDestroy : function(view, eOpts) {
		// User defined destory
		var view = this.getMainView();
		if (view.SmfrViewMaterialSnWaittingForOrderServiceStore) {
			view.SmfrViewMaterialSnWaittingForOrderServiceStore.destroyStore();
			view.SmfrViewMaterialSnWaittingForOrderServiceStore = undefined;
		}
		if (view.iOTypeStoreStore) {
			view.iOTypeStoreStore.destroyStore();
			view.iOTypeStoreStore = undefined;
		}
		if (view.SbasViewWarehouseDefinitionServiceStore) {
			view.SbasViewWarehouseDefinitionServiceStore.destroyStore();
			view.SbasViewWarehouseDefinitionServiceStore = undefined;
		}
		if (view.SmfrViewProductionOrderServiceStore) {
			view.SmfrViewProductionOrderServiceStore.destroyStore();
			view.SmfrViewProductionOrderServiceStore = undefined;
		}
		if (view.moRequestScanResultStoreStore) {
			view.moRequestScanResultStoreStore.destroyStore();
			view.moRequestScanResultStoreStore = undefined;
		}
		if (this.getMainView().input)
			this.getMainView().input = undefined;
		if (this.getMainView().output)
			this.getMainView().output = undefined;
	},
	onCdvMoSelect : function(btn, record) {
	},
	onTxtMsnSpecialkey : function(textfield, e, eOpts) {
		if (e.getKey() == e.ENTER) {
		    if(!this.getItem('frmOper').isValid()){
		        return;
		    }
		    
		    this.saveData();
		}
	},
	onBtnRefreshClick : function(button, e, eOpts) {
		if (!this.getItem('cdvMo').isValid() || !this.getItem('cmbIoType').isValid()) {
		    return;
		}
		
		this.loadMsnForMoList({
		    mo: this.getItem('cdvMo').getValue(),
		    ioType: this.getItem('cmbIoType').getValue()
		});
	},
	onCmbIoTypeSelect : function(combo, records, eOpts) {
		this.getItem('numRtnQty').setVisible(records[0].get('k') === 'I');
	},
	loadMsnForMoList : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('msnForMoList');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadIOTypeStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('IOTypeStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadWarehouseStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('WarehouseStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	saveData : function() {
		var txtMsn = this.getItem('txtMsn');
		txtMsn.setReadOnly(true);
		
		var me = this;
		var vals = me.getItem('frmOper').getValues();
		
		YX.ZbusRpc.request({
		    module: 'SmfrSaveMsnForMsnRequestService',
		    showSuccess: false,
		    showFail: false,
		    params: vals,
		    failure: function(rsp) {
		        me.getItem('dspMsg').setValue(rsp.rspDesc);
		    },
		    success: function(rsp) {
		        me.getItem('dspMsg').setValue(rsp.rspDesc);
		    },
		    callback: function(options, rspSuccess, rsp) {
		        if (rspSuccess) {
		            var vWhName = me.getItem('cmbWhCode').getStore().findRecord('whCode', vals.whCode).get('whName');
		
		            me.getItem('grdScanResult').getStore().insert(0, {
		                scanTime: Ext.Date.format(new Date(), "H:i:s"),
		                mo: vals.mo,
		                whName: vWhName,
		                matCode: rsp.rspCode == "SYS-0000" ? rsp.msnSts.matCode: "",
		                matDesc: rsp.rspCode == "SYS-0000" ? rsp.msnSts.matDesc: "",
		                ioType: vals.ioType == 'I' ? '退库': '出库',
		                msn: vals.msn,
		                qty: rsp.rspCode == "SYS-0000" ? rsp.msnSts.qty: 0,
		                result: rsp.rspCode == "SYS-0000" ? '成功': '失败',
		                resultText: rsp.rspCode == "SYS-0000" ? "": rsp.rspDesc
		            });
		        }
		
		        txtMsn.setReadOnly(false);
		        txtMsn.setValue('');
		        txtMsn.focus();
		    }
		});
	},
	loadWorkOrderStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('WorkOrderStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadMoRequestScanResultStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('MoRequestScanResultStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	}
});